<?php

class IDUtil
{
    /*
        $idleng = strlen(IDUtil::getId());
        $uuid = substr(IDUtil::getId(), 1, $idleng);
        echo IDUtil::getId();
     * */

    //生成的后面9位不一样
    public static function getId()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }

    //生成16位数
    public static function generate_id($length = 16)
    {
        $chars = '0123456789';
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $chars[rand(0, strlen($chars) - 1)];
        }
        return $id;
    }

    //生成指定位数
    public static function generateSpecifyId($length)
    {
        $chars = '123456789';
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $chars[rand(0, strlen($chars) - 1)];
        }
        return $id;
    }

    //生成指定位数加分类名称，如user用户->u05138777221148818133
    public static function generateClassSpecifyId($className,$length)
    {
        $chars = '0123456789';
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $chars[rand(0, strlen($chars) - 1)];
        }
        return $className.$id;
    }


    public static  function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';

        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }

        return $randomString;

        //echo generateRandomString(15);
    }
}

